home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / JRadioButtonMenuItem.java < prev    next >
Text File  |  1998-06-30  |  7KB  |  224 lines

  1. /*
  2.  * @(#)JRadioButtonMenuItem.java    1.19 98/02/27
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing;
  21.  
  22. import java.util.EventListener;
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import java.awt.image.*;
  26. import com.sun.java.swing.plaf.*;
  27. import com.sun.java.accessibility.*;
  28.  
  29. /**
  30.  * An implementation of a RadioButtonMenuItem. A RadioButtonMenuItem is
  31.  * a menu item that is part of a group of menu items in which only one
  32.  * item in the group can be selected. The selected item displays its
  33.  * selected state. Selecting it causes any other selected item to
  34.  * switch to the unselected state.
  35.  * Used with a ButtonGroup object to create a group of menu items
  36.  * in which only one item at a time can be selected. (Create a ButtonGroup
  37.  * object and use its <code>add</code> method to include the JRadioButtonMenuItem
  38.  * objects in the group.)
  39.  * <p>
  40.  * For the keyboard keys used by this component in the standard Look and
  41.  * Feel (L&F) renditions, see the
  42.  * <a href="doc-files/Key-Index.html#JRadioButtonMenuItem">JRadioButtonMenuItem</a> key assignments.
  43.  * <p>
  44.  * Warning: serialized objects of this class will not be compatible with
  45.  * future swing releases.  The current serialization support is appropriate
  46.  * for short term storage or RMI between Swing1.0 applications.  It will
  47.  * not be possible to load serialized Swing1.0 objects with future releases
  48.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  49.  * baseline for the serialized form of Swing objects.
  50.  *
  51.  * @beaninfo
  52.  *   attribute: isContainer false
  53.  *
  54.  * @version 1.19 02/27/98
  55.  * @author Georges Saab
  56.  * @author David Karlton
  57.  * @see ButtonGroup
  58.  */
  59. public class JRadioButtonMenuItem extends JMenuItem implements Accessible {
  60.     /**
  61.      * Creates a JRadioButtonMenuItem with no set text or icon.
  62.      */
  63.     public JRadioButtonMenuItem() {
  64.         this(null, null);
  65.     }
  66.  
  67.     /**
  68.      * Creates a JRadioButtonMenuItem with an icon.
  69.      *
  70.      * @param icon the Icon to display on the RadioButtonMenuItem.
  71.      */
  72.     public JRadioButtonMenuItem(Icon icon) {
  73.         this(null, icon);
  74.     }
  75.  
  76.     /**
  77.      * Creates a JRadioButtonMenuItem with text.
  78.      *
  79.      * @param text the text of the RadioButtonMenuItem.
  80.      */
  81.     public JRadioButtonMenuItem(String text) {
  82.         this(text, null);
  83.     }
  84.     
  85.     /**
  86.      * Creates a JRadioButtonMenuItem with the specified text
  87.      * and Icon.
  88.      *
  89.      * @param text the text of the RadioButtonMenuItem
  90.      * @param icon the icon to display on the RadioButtonMenuItem
  91.      */
  92.     public JRadioButtonMenuItem(String text, Icon icon) {
  93.         setModel(new JToggleButton.ToggleButtonModel());
  94.         init(text, icon);
  95.         setBorderPainted(false);
  96.         setFocusPainted(false);
  97.         setHorizontalTextPosition(JButton.RIGHT);
  98.         setHorizontalAlignment(JButton.LEFT);
  99.         // setArmedPainted(false);
  100.         updateUI();
  101.     }
  102.  
  103.     /**
  104.      * Initialize the JRadioButtonMenuItem with the specified text
  105.      * and Icon.
  106.      *
  107.      * @param text the text to display
  108.      * @param icon the icon to display
  109.      */
  110.     protected void init(String text, Icon icon) {
  111.         setLayout(new OverlayLayout(this));
  112.  
  113.         if(text != null) {
  114.             setText(text);
  115.             
  116.             if(icon != null) {
  117.                 setVerticalTextPosition(BOTTOM);
  118.             } 
  119.         }
  120.         
  121.         if(icon != null) {
  122.             setIcon(icon);
  123.         }
  124.         
  125.         // Listen for Focus events
  126.         addFocusListener(
  127.             new FocusListener() {
  128.             public void focusGained(FocusEvent event) {}
  129.             public void focusLost(FocusEvent event) {
  130.                 // When focus is lost, repaint if 
  131.                 // we focus information is painted
  132.                 if(isFocusPainted()) {
  133.                     repaint();
  134.                 }
  135.             }
  136.         }
  137.         );
  138.     }
  139.     
  140.     /**
  141.      * Sets the L&F object that renders this component.
  142.      *
  143.      * @param ui the RadioButtonMenuItemUI L&F object
  144.      * @see UIDefaults#getUI
  145.      * @beaninfo
  146.      * description: The menu item's UI delegate
  147.      *       bound: true
  148.      *      expert: true
  149.      *      hidden: true
  150.      */
  151.     public void setUI(RadioButtonMenuItemUI ui) {
  152.         super.setUI(ui);
  153.     }
  154.     
  155.     /**
  156.      * Notification from the UIFactory that the L&F has changed. 
  157.      * Called to replace the UI with the latest version from the 
  158.      * UIFactory.
  159.      *
  160.      * @see JComponent#updateUI
  161.      */
  162.     public void updateUI() {
  163.         setUI((RadioButtonMenuItemUI)UIManager.getUI(this));
  164.     }
  165.  
  166.  
  167.     /**
  168.      * Returns the name of the L&F class that renders this component.
  169.      *
  170.      * @return "RadioButtonMenuItemUI"
  171.      * @see JComponent#getUIClassID
  172.      * @see UIDefaults#getUI
  173.      */
  174.     public String getUIClassID() {
  175.         return "RadioButtonMenuItemUI";
  176.     }
  177.  
  178.  
  179.     /*
  180.      * Override Component.requestFocus() to not grab focus.
  181.      */
  182.     public void requestFocus() {}
  183.  
  184.  
  185. /////////////////                                                 
  186. // Accessibility support
  187. ////////////////
  188.  
  189.     /**
  190.      * Get the AccessibleContext associated with this JComponent
  191.      *
  192.      * @return the AccessibleContext of this JComponent
  193.      */
  194.     public AccessibleContext getAccessibleContext() {
  195.         if (accessibleContext == null) {
  196.             accessibleContext = new AccessibleJRadioButtonMenuItem();
  197.         }
  198.         return accessibleContext;
  199.     }
  200.  
  201.     /**
  202.      * The class used to obtain the accessible role for this object.
  203.      * <p>
  204.      * Warning: serialized objects of this class will not be compatible with
  205.      * future swing releases.  The current serialization support is appropriate
  206.      * for short term storage or RMI between Swing1.0 applications.  It will
  207.      * not be possible to load serialized Swing1.0 objects with future releases
  208.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  209.      * baseline for the serialized form of Swing objects.
  210.      */
  211.     protected class AccessibleJRadioButtonMenuItem extends AccessibleJMenuItem {
  212.         /**
  213.          * Get the role of this object.
  214.          *
  215.          * @return an instance of AccessibleRole describing the role of the 
  216.          * object
  217.          */
  218.         public AccessibleRole getAccessibleRole() {
  219.             return AccessibleRole.RADIO_BUTTON;
  220.         }
  221.     } // inner class AccessibleJRadioButtonMenuItem
  222. }
  223.  
  224.